package com.warm.pump.module.sys.controller;

import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.util.Constant;
import com.warm.pump.frame.util.RRException;
import com.warm.pump.module.sys.bean.SysMenuDto;
import com.warm.pump.module.sys.bean.po.gen.SysMenu;
import com.warm.pump.module.sys.service.ISysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统菜单
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 所有菜单列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public OutJson list(Integer page, Integer limit, Long nodeid) {
        Map<String, Object> map = new HashMap<>();
        map.put("offset", (page - 1) * limit);
        map.put("limit", limit);

        if (nodeid != null) {
            map.put("parentId", nodeid);
        } else {
            map.put("parentId", 0);
        }

        map.put("orderBy", " order_num asc ");
        // 查询列表数据
        List<SysMenu> menuList = sysMenuService.queryList(map);

        List<SysMenuDto> menuDtoList = new ArrayList<>();
        for (SysMenu menuEntity : menuList) {

            SysMenuDto menuDto = new SysMenuDto();
            try {
                MyBeanUtils.copyBean2Bean(menuDto, menuEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
            menuDto.setOpen(false);

            List<SysMenu> sysMenuEntities = sysMenuService.queryListParentId(menuEntity.getMenuId());
            if (null != sysMenuEntities && sysMenuEntities.size() > 0) {
                menuDto.setIsLeaf(false);
                menuDto.setLevel(0);
            } else {
                menuDto.setIsLeaf(true);
                menuDto.setLevel(1);
            }
            menuDtoList.add(menuDto);
        }

        return OutJson.ok().setResData(menuDtoList);
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:menu:select")
    public OutJson select() {
        // 查询列表数据
        List<SysMenu> menuList = sysMenuService.queryNotButtonList();

        // 添加顶级菜单
        SysMenuDto root = new SysMenuDto();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);

        return OutJson.ok().put("menuList", menuList);
    }

    /**
     * 角色授权菜单
     */
    @RequestMapping("/perms")
    @RequiresPermissions("sys:menu:perms")
    public OutJson perms() {
        // 查询列表数据
        List<SysMenu> menuList = sysMenuService.queryList(new HashMap<String, Object>());

        return OutJson.ok().put("menuList", menuList);
    }

    /**
     * 菜单信息
     */
    @RequestMapping("/info/{menuId}")
    @RequiresPermissions("sys:menu:info")
    public OutJson info(@PathVariable("menuId") Long menuId) {
        SysMenu menu = sysMenuService.queryObject(menuId);
        return OutJson.ok().put("menu", menu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public OutJson save(@RequestBody SysMenu menu) {
        // 数据校验
        verifyForm(menu);

        sysMenuService.save(menu);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:menu:update")
    public OutJson update(@RequestBody SysMenu menu) {
        // 数据校验
        verifyForm(menu);

        sysMenuService.update(menu);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:menu:delete")
    public OutJson delete(@RequestBody Long[] menuIds) {
//        for (Long menuId : menuIds) {
//            if (menuId.longValue() <= 28) {
//                return OutJson.error("系统菜单，不能删除");
//            }
//        }
        sysMenuService.deleteBatch(menuIds);

        return OutJson.ok();
    }

    /**
     * 用户菜单列表
     */
    @RequestMapping("/user")
    public OutJson user() {
        List<SysMenuDto> menuList = sysMenuService.getUserMenuList(getUserId());

        return OutJson.ok().put("menuList", menuList);
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenu menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new RRException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            throw new RRException("上级菜单不能为空");
        }

        // 菜单
        if (menu.getType() == Constant.MenuType.MENU.getValue()) {
            if (StringUtils.isBlank(menu.getUrl())) {
                throw new RRException("菜单URL不能为空");
            }
        }

        // 上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (menu.getParentId() != 0) {
            SysMenu parentMenu = sysMenuService.queryObject(menu.getParentId());
            parentType = parentMenu.getType();
        }

        // 目录、菜单
        if (menu.getType() == Constant.MenuType.CATALOG.getValue() || menu.getType() == Constant.MenuType.MENU.getValue()) {
            if (parentType != Constant.MenuType.CATALOG.getValue()) {
                throw new RRException("上级菜单只能为目录类型");
            }
            return;
        }

        // 按钮
        if (menu.getType() == Constant.MenuType.BUTTON.getValue()) {
            if (parentType != Constant.MenuType.MENU.getValue()) {
                throw new RRException("上级菜单只能为菜单类型");
            }
            return;
        }
    }
}
